package com.latex_demo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.Layout;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.Choreographer;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;

import com.daquexian.flexiblerichtextview.CenteredImageSpan;
import com.daquexian.flexiblerichtextview.FImageView;
import com.daquexian.flexiblerichtextview.FlexibleRichTextView;
import com.daquexian.flexiblerichtextview.LaTeXtView;
import com.daquexian.flexiblerichtextview.QuoteView;
import com.daquexian.flexiblerichtextview.TextWithFormula;
import com.facebook.react.ReactRootView;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Dynamic;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.LayoutShadowNode;
import com.facebook.react.uimanager.NativeViewHierarchyOptimizer;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.ReactShadowNode;
import com.facebook.react.uimanager.RootView;
import com.facebook.react.uimanager.RootViewManager;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIImplementation;
import com.facebook.react.uimanager.UIViewOperationQueue;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.views.scroll.ReactScrollViewManager;
import com.facebook.react.views.view.MeasureUtil;
import com.facebook.yoga.YogaMeasureFunction;
import com.facebook.yoga.YogaMeasureMode;
import com.facebook.yoga.YogaMeasureOutput;
import com.facebook.yoga.YogaNode;
import com.orhanobut.logger.Logger;


import java.lang.reflect.Method;
import java.util.List;


public class LatexViewManager extends SimpleViewManager<FlexibleRichTextView> {

    @NonNull
    @Override
    public String getName() {
        return "RCTLatexView";
    }

    @NonNull
    @Override
    protected FlexibleRichTextView createViewInstance(@NonNull ThemedReactContext reactContext) {
        return new FlexibleRichTextView(reactContext);
    }

    @Override
    public Class getShadowNodeClass() {
        return CusLayoutShadowNode.class;
    }

    @Override
    public LayoutShadowNode createShadowNodeInstance() {
        return new CusLayoutShadowNode();
    }

    @Override
    public void updateExtraData(FlexibleRichTextView view, Object extraData) {
        super.updateExtraData(view, extraData);
        Logger.e("updateExtraData %s", (String) extraData);
        view.setText((String) extraData);
    }


    public static class CusLayoutShadowNode extends LayoutShadowNode implements YogaMeasureFunction {
        private String mathText;
        private YogaNode yogaNode;

        public CusLayoutShadowNode() {
            initMeasureFunction();
        }

        private void initMeasureFunction() {
            setMeasureFunction(this);
        }


        @Override
        public long measure(YogaNode node, float width, YogaMeasureMode widthMode, float height, YogaMeasureMode heightMode) {
            yogaNode = node;

            FlexibleRichTextView cusTextView = new FlexibleRichTextView(getThemedContext());
            int widthSpec = View.MeasureSpec.makeMeasureSpec((int) width, View.MeasureSpec.EXACTLY);
            int heightSpec = View.MeasureSpec.makeMeasureSpec((int) height, View.MeasureSpec.UNSPECIFIED);
            cusTextView.setText(this.mathText);
            cusTextView.measure(widthSpec, heightSpec);
            int mw = cusTextView.getMeasuredWidth();
            int mh = cusTextView.getMeasuredHeight();

            mh = height > 0 && height < mh ? (int) height : mh;
            return YogaMeasureOutput.make(mw, mh);
        }

        @ReactProp(name = "math")
        public void setMath(String mathText) {
            this.mathText = mathText;
            markUpdated();
            if (yogaNode != null) {
                yogaNode.dirty();
            }
        }


        @Override
        public void onCollectExtraUpdates(UIViewOperationQueue uiViewOperationQueue) {
            super.onCollectExtraUpdates(uiViewOperationQueue);
            uiViewOperationQueue.enqueueUpdateExtraData(getReactTag(), this.mathText);
        }
    }


}
